package com.mryao.publishAndSubscribe;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

public class EmitLog {

    public static void main (String[] args) throws IOException, TimeoutException {
        //获取工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("192.168.231.132");
        factory.setUsername("admin");
        factory.setPassword("admin");
        //打开连接
        Connection connection = factory.newConnection();
        //打开通道
        Channel channel = connection.createChannel();
        //声明一个扇形的exchange
        channel.exchangeDeclare("logs", "fanout");
        String message = new Date().toString();
        //把消息发给exchange
        channel.basicPublish("logs","",null,message.getBytes("UTF-8"));
        System.out.println("[x] Sent " + message);
        //释放资源
        channel.close();
        connection.close();
    }
}
